// import 'dart:ffi';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:ecy_app_flutter/routes/goodscart/model/EcyGoodsCartItemModel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../ecycommon/utils/EcyRandomUtil.dart';
import '../../goods/model/EcyGoodsSkuModel.dart';

class EcyGoodsCartItem2Widget extends StatefulWidget {
  EcyGoodsCartItem2Widget(this.model, this.onOpeNumlValue);
  final EcyGoodsSkuModel model;
  _EcyGoodsCartItemWidgetState? state;
  //选择或减少数量
  final ValueChanged<EcyGoodsSkuModel> onOpeNumlValue;
  @override
  State<StatefulWidget> createState() {
    _EcyGoodsCartItemWidgetState _state =
        _EcyGoodsCartItemWidgetState(this.model, this.onOpeNumlValue);
    state = _state;
    return _state;
  }
}

class _EcyGoodsCartItemWidgetState extends State {
  EcyGoodsSkuModel model;
  final ValueChanged<EcyGoodsSkuModel> onOpeNumlValue;
  _EcyGoodsCartItemWidgetState(this.model, this.onOpeNumlValue);
  @override
  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {},
      child: Container(
        padding: EdgeInsets.only(left: 0.w, right: 0.w, top: 8.w, bottom: 8.w),
        height: 110.w,
        child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                  width: 80.w,
                  height: 80.w,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(16.r)),
                    color: Colors.white, //设置背景色为黑色
                  ),
                  child: CachedNetworkImage(
                      width: 80.w,
                      height: 80.w,
                      imageUrl: model.imageUrl ?? "",
                      imageBuilder: (context, imageProvider) => Container(
                            decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(8.r)),
                                image: DecorationImage(
                                    image: imageProvider, fit: BoxFit.cover)),
                          ),
                      // placeholder: (context, url) => CircularProgressIndicator(),
                      errorWidget: (context, url, error) => Container(
                          decoration: BoxDecoration(
                            borderRadius:
                                BorderRadius.all(Radius.circular(8.r)),
                            color: Colors.white, //设置背景色为黑色
                          ),
                          child:
                              Image.asset("assets/images/app_icon1024.png")))),
              SizedBox(width: 8.w),
              Expanded(
                  child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        model.commodityName ?? model.skuName ?? "",
                        textAlign: TextAlign.start,
                        maxLines: 2,
                        style: TextStyle(
                            fontFamily: "PingFang SC",
                            color: const Color(0xFF262626),
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w700,
                            overflow: TextOverflow.ellipsis
                        ),
                      ),
                      Text(
                        model.skuName ?? "",
                        textAlign: TextAlign.start,
                        style: TextStyle(
                            fontFamily: "PingFang SC",
                            color: const Color(0xFF8C8C8C),
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w500,
                            overflow: TextOverflow.ellipsis),
                      ),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Center(
                          child: Text(
                        "¥${model.salePrice ?? model.marketPrice ?? 0}",
                        style: TextStyle(
                            color: const Color(0xFF262626),
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w900),
                      )),
                      Wrap(
                        direction: Axis.horizontal,
                        spacing: 4.w,
                        crossAxisAlignment: WrapCrossAlignment.center,
                        children: [
                          GestureDetector(
                              onTap: () {
                                if (onOpeNumlValue != null) {
                                  if (model.selectNum != null) {
                                    if (model.selectNum! > 1) {
                                      model.selectNum = (model.selectNum! - 1);
                                      onOpeNumlValue(model);
                                      setState(() {
                                        model = model;
                                      });
                                    } else {
                                      EasyLoading.showToast("最少购买1件哦~", duration: const Duration(seconds: 2));
                                    }
                                  }
                                }
                              },
                              child: Container(
                                  width: 24.w,
                                  height: 24.w,
                                  decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(4.r)),
                                    color: const Color(0xFFF5F5F5), //设置背景色为黑色
                                  ),
                                  child: Center(
                                      child: Text(
                                    "-",
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: const Color(0xD9000000),
                                        fontSize: 14.sp,
                                        fontWeight: FontWeight.w400),
                                  )))),
                          GestureDetector(
                              onTap: () {},
                              child: Container(
                                  width: 36.w,
                                  height: 24.w,
                                  decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(4.r)),
                                    color: Color(0xFFF5F5F5), //设置背景色为黑色
                                  ),
                                  child: Center(
                                      child: Text(
                                    textAlign: TextAlign.center,
                                    (model.selectNum != null
                                        ? model.selectNum!.toInt().toString()
                                        : "0"),
                                    style: TextStyle(
                                        color: Color(0xD9000000),
                                        fontSize: 14.sp,
                                        fontWeight: FontWeight.w400),
                                  )))),
                          GestureDetector(
                              onTap: () {
                                if (onOpeNumlValue != null) {
                                  if (model.selectNum != null) {
                                    var maxNum = (model.stockQuantity ?? 0.00).toInt();
                                    var selectNum = (model.selectNum ?? 0.00).toInt();
                                    if(maxNum <= selectNum) {
                                      EasyLoading.showToast("最多购买${maxNum == 0 ? selectNum : maxNum}件哦~",
                                          duration: const Duration(seconds: 2));
                                      return;
                                    }
                                    model.selectNum = model.selectNum! + 1;
                                    onOpeNumlValue(model);
                                    setState(() {
                                      model = model;
                                    });
                                  } else {
                                    model.selectNum = 1;
                                    onOpeNumlValue(model);
                                    setState(() {
                                      model = model;
                                    });
                                  }
                                }
                              },
                              child: Container(
                                  width: 24.w,
                                  height: 24.w,
                                  decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(4.r)),
                                    color: Color(0xFFF5F5F5), //设置背景色为黑色
                                  ),
                                  child: Center(
                                      child: Text(
                                    "+",
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: Color(0xD9000000),
                                        fontSize: 14.sp,
                                        fontWeight: FontWeight.w400),
                                  ))))
                        ],
                      )
                    ],
                  )
                ],
              ))
            ]),
      ),
    );
  }

  @override
  void dispose() {
    // 清理控制器，避免内存泄漏
    super.dispose();
  }
}
